<template>
  <a-drawer :open="visible" title="采购单详情" @close="onClose">
    <template #closeIcon>
      <div class="express-bar"></div>
      <div class="express-btn">
        <CloseCircleOutlined :size="32" @click="onClose" />
      </div>
    </template>
    <a-card size="small" title="基本信息">
      <a-descriptions size="small" :column="3">
        <a-descriptions-item label="单据编号">{{ options.no }}</a-descriptions-item>
        <a-descriptions-item label="供应商">{{ options.supplier?.name }}</a-descriptions-item>
        <a-descriptions-item label="采购时间">{{ options.create_time }}</a-descriptions-item>
        <a-descriptions-item label="订单状态">
          <span>{{ getDescByValue('APPROVAL_STA_ENUM', options.purchase_status) }}</span>
        </a-descriptions-item>
        <a-descriptions-item label="订单总额">{{ Number(options?.total_price).toFixed(2) }}</a-descriptions-item>
        <a-descriptions-item label="备注">{{ options.remark }}</a-descriptions-item>
        <a-descriptions-item label="单据日期">{{ options.create_time }}</a-descriptions-item>
        <a-descriptions-item label="打印次数">{{ options?.printLog?.print_count }}</a-descriptions-item>
        <a-descriptions-item label="最近打印时间">{{ options?.printLog?.update_time }}</a-descriptions-item>
        <a-descriptions-item label="最近打印人">{{ options?.printLog?.printerName }}</a-descriptions-item>
      </a-descriptions>
    </a-card>
    <a-card class="smart-margin-top10" size="small" :tab-list="tabList" :active-tab-key="activeKey" @tabChange="(key) => (activeKey = key)">
      <template #customTab="{ tab }">
        <span class="ant-card-head-title" :style="compactModeClass">
          {{ tab }}
        </span>
      </template>
      <template v-if="activeKey === 'store'">
        <ProcurementInventoryTable :enterpriseId="options.detail" />
      </template>
      <template v-if="activeKey === 'log'">
        <ProcurementTracerTimeline :tableData="options.relatedLog" />
      </template>
    </a-card>
    <template #footer>
      <a-button @click="onClose">取消</a-button>
    </template>
  </a-drawer>
</template>

<script setup lang="ts">
import ProcurementInventoryTable from './procurement-Inventory-table.vue'
import ProcurementTracerTimeline from './procurement-tracer-timeline.vue'
import { useEnums } from '@/hooks/plugins/useEnums'
const compactModeClass = inject('compactModeClass')
const { getDescByValue } = useEnums()
defineExpose({
  showDrawer
})
const emit = defineEmits(['refresh'])

// --------------------- Drawer 显示与隐藏 ---------------------
// 是否展示
const visible = ref(false)
// 详情
const options = ref({})
// 详情tab
const activeKey = ref('store')
const tabList = [
  {
    key: 'store',
    tab: '商品明细'
  },
  {
    key: 'log',
    tab: '日志记录'
  }
]

function showDrawer(item) {
  options.value = item
  visible.value = true
}

const onClose = () => {
  visible.value = false
}
</script>
